package android.slc.appbase.api.slc.utils;


import android.slc.appbase.api.slc.callback.po.SlcEntity;

import com.blankj.utilcode.util.JsonUtils;

import okhttp3.ResponseBody;
import retrofit2.HttpException;
import retrofit2.Response;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/03/04 下午 7:57
 */
public class SlcApiUtils {
    public static <T> SlcEntity<T> getMsgByErrorBody(HttpException httpException) {
        SlcEntity<T> slcEntity = new SlcEntity<>();
        slcEntity.setCode(httpException.code());
        slcEntity.setMsg(httpException.message());
        Response<?> response = httpException.response();
        if (response != null) {
            try {
                ResponseBody errorBody = response.errorBody();
                String errorInfo = errorBody.string();
                slcEntity.setMsg(JsonUtils.getString(errorInfo, "msg"));
            } catch (Exception ioException) {
            }
        }
        return slcEntity;
    }
}
